﻿using MN.Framework.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MN.Framework.UI.Win.Forms
{
    public class MnLOVForm<EntityType, ResultType> : MnBaseForm where EntityType : BaseEntity
    {
        BaseContext DBContext { get; set; }
        BaseEntity Entity { get; set; }
        public ResultType Result { get; set; }
        private MnGridView<EntityType> gridMain;
        public MnLOVForm(BaseContext context, BaseEntity entity = null)
        {
            Entity = entity;
            DBContext = context;
            InitializeComponent();
        }

       
        private void InitializeComponent()
        {
            this.gridMain = new MnGridView<EntityType>(DBContext, Entity);
            this.gridMain.DoubleClick += gridMain_DoubleClick;
            this.gridMain.AllowAddNewRow = false;
            this.gridMain.AllowDeleteRow = false;
            this.gridMain.AllowEditRow = false;


            ((System.ComponentModel.ISupportInitialize)(this)).BeginInit();
            this.SuspendLayout();
            
            this.ClientSize = new System.Drawing.Size(292, 270);
            this.Name = "MnLOV";
           
            this.RootElement.ApplyShapeToControl = true;
            this.Controls.Add(gridMain);
            ((System.ComponentModel.ISupportInitialize)(this)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        void gridMain_DoubleClick(object sender, EventArgs e)
        {
            var selectedID = this.gridMain.SelectedRows[0].Cells["ID"].Value;
            Result = (ResultType)selectedID;
            this.Close();
        }
    }
}
